{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 交叉熵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From <ipython-input-2-a101e9476d65>:20: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n",
      "Iter0, Testing Accuracy:0.8131\n",
      "Iter1, Testing Accuracy:0.8246\n",
      "Iter2, Testing Accuracy:0.8731\n",
      "Iter3, Testing Accuracy:0.8941\n",
      "Iter4, Testing Accuracy:0.8986\n",
      "Iter5, Testing Accuracy:0.9024\n",
      "Iter6, Testing Accuracy:0.9036\n",
      "Iter7, Testing Accuracy:0.9058\n",
      "Iter8, Testing Accuracy:0.9072\n",
      "Iter9, Testing Accuracy:0.9079\n",
      "Iter10, Testing Accuracy:0.9095\n",
      "Iter11, Testing Accuracy:0.9099\n",
      "Iter12, Testing Accuracy:0.9107\n",
      "Iter13, Testing Accuracy:0.9118\n",
      "Iter14, Testing Accuracy:0.9127\n",
      "Iter15, Testing Accuracy:0.9132\n",
      "Iter16, Testing Accuracy:0.9144\n",
      "Iter17, Testing Accuracy:0.9146\n",
      "Iter18, Testing Accuracy:0.9154\n",
      "Iter19, Testing Accuracy:0.9155\n",
      "Iter20, Testing Accuracy:0.917\n",
      "Iter21, Testing Accuracy:0.918\n",
      "Iter22, Testing Accuracy:0.9177\n",
      "Iter23, Testing Accuracy:0.9177\n",
      "Iter24, Testing Accuracy:0.9183\n",
      "Iter25, Testing Accuracy:0.9187\n",
      "Iter26, Testing Accuracy:0.9192\n",
      "Iter27, Testing Accuracy:0.9191\n",
      "Iter28, Testing Accuracy:0.9189\n",
      "Iter29, Testing Accuracy:0.9201\n",
      "Iter30, Testing Accuracy:0.92\n",
      "Iter31, Testing Accuracy:0.9201\n",
      "Iter32, Testing Accuracy:0.9206\n",
      "Iter33, Testing Accuracy:0.9211\n",
      "Iter34, Testing Accuracy:0.9209\n",
      "Iter35, Testing Accuracy:0.9215\n",
      "Iter36, Testing Accuracy:0.921\n",
      "Iter37, Testing Accuracy:0.9216\n",
      "Iter38, Testing Accuracy:0.9214\n",
      "Iter39, Testing Accuracy:0.9212\n",
      "Iter40, Testing Accuracy:0.9218\n",
      "Iter41, Testing Accuracy:0.9219\n",
      "Iter42, Testing Accuracy:0.9221\n",
      "Iter43, Testing Accuracy:0.9223\n",
      "Iter44, Testing Accuracy:0.9219\n",
      "Iter45, Testing Accuracy:0.9224\n",
      "Iter46, Testing Accuracy:0.9221\n",
      "Iter47, Testing Accuracy:0.9227\n",
      "Iter48, Testing Accuracy:0.9228\n",
      "Iter49, Testing Accuracy:0.9233\n",
      "completed\n"
     ]
    }
   ],
   "source": [
    "# 载入数据集\n",
    "mnist=input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "# 每个批次的大小\n",
    "batch_size=200 # 每次放入的数据量\n",
    "# 计算有多少个批次\n",
    "n_batch=mnist.train.num_examples // batch_size\n",
    "\n",
    "# 定义两个placeholder\n",
    "x=tf.placeholder(tf.float32,[None,784])\n",
    "y=tf.placeholder(tf.float32,[None,10])\n",
    "\n",
    "# 创建简单的神经网络\n",
    "W=tf.Variable(tf.zeros([784,10]))\n",
    "b=tf.Variable(tf.zeros([10]))\n",
    "prediction=tf.nn.softmax(tf.matmul(x,W)+b)\n",
    "\n",
    "# 二次代价函数\n",
    "# loss=tf.reduce_mean(tf.square(y-prediction))\n",
    "loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))\n",
    "\n",
    "# 使用梯度下降法\n",
    "train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "# 记过存放在布尔型列表中\n",
    "correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) # 最大值所在位置\n",
    "# 求准确率\n",
    "accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for epoch in range(50):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys=mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})\n",
    "        \n",
    "        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print(\"Iter\"+str(epoch)+\", Testing Accuracy:\"+str(acc))\n",
    "\n",
    "print('completed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
